package com.service.business.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.service.business.base.dto.LoginUserSession;
import com.service.business.framework.log.RequestLog;
import com.service.business.member.controller.rep.MemberSignInfo;
import com.service.business.member.controller.rep.MemberSignRep;
import com.service.business.member.controller.req.ListSignReq;
import com.service.business.member.service.MemberJAService;
import com.service.business.member.service.MemberSignService;
import com.service.business.member.service.MemberStoreService;
import com.service.business.support.SessionUtil;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.BaseResponse;
import com.service.core.rest.response.OneResponse;
import com.service.dao.enums.CoinsEnum;
import com.service.dao.enums.MemberJAType;
import com.service.dao.model.MemberSign;
import com.service.dao.model.MemberStore;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("member-daily_sign")
public class MemberDailySignController extends BaseController {

	@Autowired
	private MemberStoreService memberStoreService;
	@Autowired
	private SessionUtil sessionUtil;
	@Autowired
	private MemberSignService memberSignService;
	@Autowired
	private MemberJAService memberJAService;

	@ApiOperation("查询用户积分")
	@RequestLog("查询用户积分")
	@PostMapping("points")
	public OneResponse<MemberSignInfo> points() {
		MemberSignInfo signInfo = new MemberSignInfo();
		LoginUserSession sessionUser = sessionUtil.getUser();
		MemberStore memberStore = memberStoreService.getMemberStore(sessionUser.getUserId(), CoinsEnum.POINTS.getName());
		if (ObjectUtils.isEmpty(memberStore)) {
			signInfo.setTotalPoints(BigDecimal.ZERO);
		} else {
			signInfo.setTotalPoints(memberStore.getNum());
		}
		MemberSign memberSign = memberSignService.getOne(new QueryWrapper<MemberSign>().lambda().eq(MemberSign::getMemberId, sessionUser.getUserId()));
		if (ObjectUtils.isEmpty(memberSign)) {
			signInfo.setCountContinueSign(0);
		} else {
			signInfo.setCountContinueSign(memberSign.getCountContinueSign());
		}
		String currentDateStr = DateFormatUtils.format(new Date(), "yyyy-MM-dd");
		Integer num = memberJAService.checkByMemberAndTypeAndSomeday(sessionUser.getUserId(), currentDateStr, MemberJAType.getSignType());
		if (num > 0) {
			signInfo.setIsTodaySign(1);
		} else {
			signInfo.setIsTodaySign(0);
		}
		signInfo.setTodayPoints(memberSignService.getSomedayByMember(sessionUser.getUserId(), new Date()));//设置今日获取积分
		return successQueryOneResponse(signInfo);
	}

	@ApiOperation("每日签到")
	@RequestLog("每日签到")
	@PostMapping("sign")
	public BaseResponse dailySign() {
		LoginUserSession sessionUser = sessionUtil.getUser();
		memberSignService.sign(sessionUser.getUserId());
		return successBaseResponse();
	}

	@ApiOperation("每月签到列表")
	@RequestLog("每月签到列表")
	@PostMapping("month-sign/{month}")
	public OneResponse<MemberSignRep> monthSign(@PathVariable("month") String month) {
		LoginUserSession sessionUser = sessionUtil.getUser();
		String fullDate = month + "-01 00:00:00";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date monthDate = null;
		try {
			monthDate = sdf.parse(fullDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return successQueryOneResponse(memberSignService.listByMemberIdAndSomeday(sessionUser.getUserId(), monthDate));
	}
}
